package com.itheima.zipstream;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipInputStreamDemo {
    public static void main(String[] args) throws IOException {
        //将压缩文件里的内容解压缩
        File src = new File("D:\\aaa.zip");

        File dest = new File("D:\\");

        unzip(src,dest);
    }

    private static void unzip(File src, File dest) throws IOException {
        ZipInputStream zis = new ZipInputStream(new FileInputStream(src));

        ZipEntry entry;
        while ((entry=zis.getNextEntry())!=null){
            if (entry.isDirectory()){
                //获取到的zipentry对象时文件夹
                File f = new File(dest,entry.toString());
                f.mkdirs();
            }else {
                //获取到的zipentry对象是文件
                File f = new File(dest,entry.toString());
                FileOutputStream fos = new FileOutputStream(f);
                int b;
                while ((b=zis.read())!=-1){
                    fos.write(b);
                }
                fos.close();
                //表示当前条目文件已读取完毕
                zis.closeEntry();
            }
        }
        zis.close();
    }
}
